import { ExecutionContext, SetMetadata, createParamDecorator } from "@nestjs/common";
import { Request } from 'express'
export enum PERMISSION_KEY {
    login = 'require-login',
    permission = 'require-permission'
}

export const RequireLogin = () => SetMetadata(PERMISSION_KEY.login, true);
export const RequirePermission = (...permissions: string[]) => SetMetadata(PERMISSION_KEY.permission, permissions);

export const UserInfo = createParamDecorator((data: string, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest<Request>();
    if (!request.user) {
        return null
    }
    return data ? request.user[data] : request.user
})